
a = {'zero':0, 'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8, 'nine':9}
a2 = 'zero one two three four five six seven eight nine'
ch = input()
chm = ch.split(' ')
c = 0
m = 0
p = 0
l = []
if len(chm) == 3 and chm[2] != "":
    if chm[0] not in a or chm[1] not in a or chm[2] not in a:
        print('Wrong input')
    else:
        chf = a[chm[0]] * 100 + a[chm[1]] * 10 + a[chm[2]]
        bl1 = chf
        c = chf
        p = chf
        bl2 = 0        
        for i in range(2, 1400):
            for t in range(2, i):
                if i % t == 0:
                    m = 0
                    break
                else:
                    m = i
                    continue
            if m != 0:
                l.append(m)
        while True:
            if c in l and c != chf:
                break
            else:
                c = c + 1
        while True:
            if p in l and p != chf:
                break
            else:
                p = p - 1
        if chf - p > c - chf:
            print(c)
        else:
            if chf - p == c - chf:
                print(p)
            else:
                print(p)
else:
    print("Wrong input")